package com.fanfanwang.ffw.app.ui.my;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.common.StringUtils;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;

/**
 * 离线地图
 * @author Administrator
 *
 */
public class MyOffLineAct extends BaseActivity implements MKOfflineMapListener{

	private CommonTitleBar commonTitleBar;
	private Activity mAct;
	private Intent mIntent;
	private Button startBtn;
	private TextView currPrecent;//当前百分比
	private TextView currSize;//当前下载的大小
	private TextView sumSize;//文件的总大小
	private MKOfflineMap mOffline = null;
	private ProgressBar pb_group;
	
	/**
	 * 已下载的离线地图信息列表
	 */
	private ArrayList<MKOLUpdateElement> localMapList = null;
	private LocalMapAdapter lAdapter = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
		AppManager.getAppManager().addActivity(this);
		initData();
		setListener();
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_my_offline);
		mAct = this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		startBtn = (Button) findViewById(R.id.startBtn);
		currPrecent = (TextView) findViewById(R.id.now_progress);
		currSize = (TextView) findViewById(R.id.tv_now);
		sumSize = (TextView) findViewById(R.id.tv_sum);
		pb_group = (ProgressBar) findViewById(R.id.pb_group);
		mOffline = new MKOfflineMap();
		mOffline.init(this);
		
		// 获取已下过的离线地图信息
		localMapList = mOffline.getAllUpdateInfo();
		if (localMapList == null) {
			localMapList = new ArrayList<MKOLUpdateElement>();
		}

		ListView localMapListView = (ListView) findViewById(R.id.localmaplist);
		lAdapter = new LocalMapAdapter();
		localMapListView.setAdapter(lAdapter);
	}

	@Override
	public void setListener() {
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		startBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startBtn.setBackgroundColor(Color.GRAY);
				startBtn.setClickable(false);
				int cityid=150;
				mOffline.start(cityid);
//				clickLocalMapListButton(null);
				ToastAlone.showToast(mAct, "开始下载离线地图. cityid: " + cityid)
						.show();
				updateView();
			}
		});

	}
public void updateView(){
	/*MKOLUpdateElement e=mOffline.getAllUpdateInfo().get(0);
	currPrecent.setText(e.ratio);
	currSize.setText(e.size);
	sumSize.setText(e.serversize);
	pb_group.setProgress(e.ratio*100);*/
	localMapList = mOffline.getAllUpdateInfo();
	if (localMapList == null) {
		localMapList = new ArrayList<MKOLUpdateElement>();
	}
	lAdapter.notifyDataSetChanged();
	
}
	@Override
	public void initData() {
		// TODO Auto-generated method stub
		commonTitleBar.setCenterText(getString(R.string.my_offline_map));
		commonTitleBar.setReturnVisible();
	}

	@Override
	public void onGetOfflineMapState(int type, int state) {
		// TODO Auto-generated method stub
		switch (type) {
		case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: {
			MKOLUpdateElement update = mOffline.getUpdateInfo(state);
			// 处理下载进度更新提示
			if (update != null) {
//				currPrecent.setText(String.format("%s : %d%%", update.cityName,
//						update.ratio));
				currPrecent.setText(String.format("%s : %d%%", "",
						update.ratio));
				sumSize.setText((update.serversize/1024/1024)+"");
				currSize.setText((update.size/1024/1024)+"");
				pb_group.setProgress(update.ratio);
				updateView();
			}
		}
			break;
		case MKOfflineMap.TYPE_NEW_OFFLINE:
			// 有新离线地图安装
			Log.d("OfflineDemo", String.format("add offlinemap num:%d", state));
			break;
		case MKOfflineMap.TYPE_VER_UPDATE:
			// 版本更新提示
			// MKOLUpdateElement e = mOffline.getUpdateInfo(state);

			break;
		}
	}

	/*public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
		}
		return super.onKeyDown(keyCode, event);
	}
	 */

	/**
	 * 离线地图管理列表适配器
	 */
	public class LocalMapAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return localMapList.size();
		}

		@Override
		public Object getItem(int index) {
			return localMapList.get(index);
		}

		@Override
		public long getItemId(int index) {
			return index;
		}

		@Override
		public View getView(int index, View view, ViewGroup arg2) {
			MKOLUpdateElement e = (MKOLUpdateElement) getItem(index);
			view = View.inflate(MyOffLineAct.this,
					R.layout.offline_localmap_list, null);
			initViewItem(view, e);
			return view;
		}

		void initViewItem(View view, final MKOLUpdateElement e) {
			Button display = (Button) view.findViewById(R.id.display);
			Button remove = (Button) view.findViewById(R.id.remove);
			TextView title = (TextView) view.findViewById(R.id.title);
			TextView update = (TextView) view.findViewById(R.id.update);
			TextView ratio = (TextView) view.findViewById(R.id.ratio);
			ratio.setText(e.ratio + "%");
			title.setText(e.cityName);
			if (e.update) {
				update.setText("可更新");
			} else {
				update.setText("最新");
			}
			if (e.ratio != 100) {
				display.setEnabled(false);
			} else {
				display.setEnabled(true);
			}
			remove.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					mOffline.remove(e.cityID);
					updateView();
				}
			});
/*			display.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.putExtra("x", e.geoPt.longitude);
					intent.putExtra("y", e.geoPt.latitude);
					intent.setClass(MyOffLineAct.this, BaseMapDemo.class);
					startActivity(intent);
				}
			});*/
		}

	}
}
